package dq.study.designmodel.observer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author dongqun
 * @Description: 孩子：被观察者
 * @date 2019/4/15 22:13
 */
public class Child implements Runnable {

    private List<SleepListener> list = new ArrayList<SleepListener>();

    public void deleteListener(SleepListener sleepListener) {
        list.remove(sleepListener);
    }

    public void addListener(SleepListener sleepListener) {
        list.add(sleepListener);
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000L);
            Event event = new Event("我睡醒了",new Date(),"bed");
            performAction(event);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void performAction(Event event) {
        for (SleepListener sleepListener : list) {
            sleepListener.doAction(event);
        }
    }
}
